#!/bin/sh

mkdir -p build

echo "Time results"                  > build/build_time.txt
echo "------------"                 >> build/build_time.txt
echo ""                             >> build/build_time.txt
echo "Started at:"                  >> build/build_time.txt
date +'%H:%M:%S %d.%m.%Y'           >> build/build_time.txt
echo ""                             >> build/build_time.txt
echo "STAGE    | CPU  | TIME    "   >> build/build_time.txt
echo "--------------------------"   >> build/build_time.txt

cd build

echo
echo "  \e[44;36;1m                   \e[0m  "
echo "  \e[44;36;1m .--== CONAN ==--. \e[0m  "
echo "  \e[44;36;1m                   \e[0m  "
echo
conan profile detect --force
time -f "CONAN    | %P\t| %E" --quiet -o build_time.txt -a \
    conan install ../conanfile.py --build=missing -s compiler.libcxx=libstdc++11 -of=conan/
if [ $? -ne 0 ]; then
    echo "\e[41;33;1mЭтап CONAN завершился с ошибкой!\e[0m"
    exit
fi

echo
echo "  \e[44;36;1m                   \e[0m  "
echo "  \e[44;36;1m .--== CMAKE ==--. \e[0m  "
echo "  \e[44;36;1m                   \e[0m  "
echo
time -f "CMAKE    | %P\t| %E" --quiet -o build_time.txt -a \
    cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_ALL=ON -DCMAKE_TOOLCHAIN_FILE=conan/conan_toolchain.cmake
if [ $? -ne 0 ]; then
    echo "\e[41;33;1mЭтап CMAKE завершился с ошибкой!\e[0m"
    exit
fi

echo
echo "  \e[44;36;1m                   \e[0m  "
echo "  \e[44;36;1m .--== BUILD ==--. \e[0m  "
echo "  \e[44;36;1m                   \e[0m  "
echo
time -f "BUILD    | %P\t| %E" --quiet -o build_time.txt -a \
    make -j5
if [ $? -ne 0 ]; then
    echo "\e[41;33;1mЭтап BUILD завершился с ошибкой!\e[0m"
    exit
fi

echo
echo "  \e[44;36;1m                      \e[0m  "
echo "  \e[44;36;1m .--== INSLTALL ==--. \e[0m  "
echo "  \e[44;36;1m                      \e[0m  "
echo
time -f "INSLTALL | %P\t| %E" --quiet -o build_time.txt -a \
    make install
if [ $? -ne 0 ]; then
    echo "\e[41;33;1mЭтап INSLTALL завершился с ошибкой!\e[0m"
    exit
fi

echo
echo "  \e[44;36;1m                  \e[0m  "
echo "  \e[44;36;1m .--== TEST ==--. \e[0m  "
echo "  \e[44;36;1m                  \e[0m  "
echo
time -f "TEST     | %P\t| %E" --quiet -o build_time.txt -a \
    ctest

echo ""                     >> build_time.txt
echo "Finished at:"         >> build_time.txt
date +'%H:%M:%S %d.%m.%Y'   >> build_time.txt

echo
cat build_time.txt
